package com.api.devices.service.impl;

import com.api.common.annotation.DataSource;
import com.api.common.enums.DataSourceType;
import com.api.devices.domain.query.IpcQuery;
import com.api.devices.domain.query.ResourceQuery;
import com.api.devices.domain.vo.IpcVo;
import com.api.devices.mapper.ResourceMapper;
import com.api.devices.service.IResourceService;
import com.api.framework.datasource.DynamicDataSourceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResourceServiceImpl implements IResourceService {
    @Autowired
    ResourceMapper resourceMapper;

    //    @Override
//    public ResourceQuery latestNews(String tableName) {
//        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
//        ResourceQuery query = resourceMapper.resourceLatest(tableName);
//        DynamicDataSourceContextHolder.clearDataSourceType();
//        return query;
//    }
    @Override
    public ResourceQuery latestNews(String tableName) {

        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        ResourceQuery query = new ResourceQuery();
        try {
            query = resourceMapper.latestNews(tableName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return query;
    }

    @Override
//    @DataSource(value = DataSourceType.SLAVE)
    public ResourceQuery latest(String ipcCode) {
//        return resourceMapper.latestNews(IpcVo.getResourceTableName(ipcCode));
        ResourceQuery query = new ResourceQuery();
        try {
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
            query = resourceMapper.latestNews(IpcVo.getResourceTableName(ipcCode));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return query;
    }
//    public Map<String,ResourceQuery> latest(List<String> list) {
//        Map<String , ResourceQuery> map = new HashMap<>();
//        for(String ipcCode: list){
//            ResourceQuery query = resourceMapper.latestNews(IpcVo.getResourceTableName(ipcCode));
//            map.put(ipcCode,query);
//        }
//        return map;
//    }

    @Override
    public List<ResourceQuery> resourceList(IpcVo vo) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<ResourceQuery> queryList = new ArrayList<>();
        try {
            queryList = resourceMapper.resourceList(vo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return queryList;
    }
}
